package entityclasses;

import socialappexception.FriendRequestException;
import java.util.*;
import javax.swing.JOptionPane;
import visitors.IShowPersonsWithGroupsVisitor;
import factories.VectorFactory;

public class Group {

    public Group() {
        subGroups = VectorFactory.createVector(Group.class);
        persons = VectorFactory.createVector(Person.class);
        subGroupPersons
                = VectorFactory.createVector(Person.class);
    }

    public Group(String name) {
        this.name = name;
        subGroups = VectorFactory.createVector(Group.class);
        persons = VectorFactory.createVector(Person.class);
        subGroupPersons
                = VectorFactory.createVector(Person.class);
    }

    private String name;
    private Vector<Group> subGroups;
    private Vector<Person> persons;
    private Vector<Person> subGroupPersons;

    public Vector<Person> getPersons() {
        return persons;
    }
    public void setPersons(Vector<Person> persons) {
        this.persons = persons;
    }
    
    public String accept(IShowPersonsWithGroupsVisitor v)
    {
        return v.visit(this);
    }
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Vector<Group> getSubgroups() {
        return subGroups;
    }

    public void setSubgroups(Vector<Group> subgroups) {
        this.subGroups = subgroups;
    }
    
    public void addSubGroup(Group g)
    {
        try {
            if(!subGroups.contains(g))
            subGroups.add(g);
            else 
            throw new FriendRequestException(g.getName() + " grubu zaten var.");
        } catch (FriendRequestException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage(), "Hata", JOptionPane.ERROR_MESSAGE);
        }
    }
    
    public void addPerson(Person p)
    {
        try {
            if(!this.persons.contains(p))
            this.persons.add(p);
            else 
            throw new FriendRequestException(p.getFirstName()+" "+ p.getLastName() + " kisisi zaten var.");
        } catch (FriendRequestException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage(), "Hata", JOptionPane.ERROR_MESSAGE);
        }
    }
    
    @Override
    public String toString()
    {
        return getName();
    }
}
